﻿using Engine;
using Engine.Graphics;
using Game;
namespace Mekiasm
{
    public class FluidBaseProgress : CanvasWidget
    {
        public ImageWidget Background;
        public ImageWidget Background2;
        public ImageWidget ProgressImage;
        public Vector2 startCoord;
        public Vector2 endCoord;
        public bool isHorizontal;
        private ComponentEnergyMachine.Slot Slot;
        Color renderColor = Color.White;
        public FluidBaseProgress(Vector2 size, ComponentEnergyMachine.Slot slot)
        {
            Slot = slot;
            Size = size;
            Matrix matrix = FluidSystem.GetFluidRenderMatrix(ref renderColor, Slot.FuelValue);
            startCoord = Vector2.Transform(Vector2.Zero, matrix);
            endCoord = Vector2.Transform(Vector2.One, matrix);
            Background = new ImageWidget();
            Background.Texture = ContentManager.Get<Texture2D>("Mekiasm/Textures/Model/guigaugestandard");
            Background.startPoint = new Vector2(0.07f, 0f);
            Background.endPoint = new Vector2(0.225f, 0.235f);
            Background.Size = size;

            Background2 = new ImageWidget();
            Background2.Texture = Background.Texture;
            Background2.startPoint = new Vector2(0f, 0f);
            Background2.endPoint = new Vector2(0.07f, 0.235f);
            Background2.Size = size;
            ProgressImage = new ImageWidget();
            ProgressImage.startPoint = startCoord;
            ProgressImage.endPoint = startCoord;
            ProgressImage.Size = size;
            Children.Add(Background2);
            Children.Add(ProgressImage);
            Children.Add(Background);
            setHAlign(false);
            SetProgress(0f);
        }

        public void setHAlign(bool isH)
        {
            isHorizontal = isH;
            if (isH)
            {
                HorizontalAlignment = WidgetAlignment.Near;
                VerticalAlignment = WidgetAlignment.Near;
            }
            else
            {
                HorizontalAlignment = WidgetAlignment.Far;
                VerticalAlignment = WidgetAlignment.Center;
            }
        }

        public void SetProgress(float f)
        {
            FluidSystem.GetFluidRenderMatrix(ref renderColor, Slot.FuelValue);
            ProgressImage.Texture = FluidSystem.GetFluidRenderTexture(Slot.FuelValue);
            ProgressImage.Color = renderColor;
            if (isHorizontal)
            {
                ProgressImage.HorizontalAlignment = WidgetAlignment.Near;
                ProgressImage.VerticalAlignment = WidgetAlignment.Center;
                Background.HorizontalAlignment = WidgetAlignment.Near;
                Background.VerticalAlignment = WidgetAlignment.Center;
                ProgressImage.Size = new Vector2(Size.X * f, Size.Y);
            }
            else
            {
                //进度条由下向上
                ProgressImage.HorizontalAlignment = WidgetAlignment.Center;
                ProgressImage.VerticalAlignment = WidgetAlignment.Far;
                Background.HorizontalAlignment = WidgetAlignment.Center;
                Background.VerticalAlignment = WidgetAlignment.Far;
                ProgressImage.Size = new Vector2(Size.X, Size.Y * f);
            }
            ProgressImage.endPoint = startCoord + (endCoord - startCoord) * f;
        }

        public override void Update()
        {
            if (Input.Press.HasValue)
            {
                if (HitTest(Input.Press.Value))
                {
                    if (Slot.FuelAmount > 0)
                    {
                        string name = BlocksManager.Blocks[Terrain.ExtractContents(Slot.FuelValue)].GetDisplayName(null, Slot.FuelValue);
                        string type = string.Empty;
                        string remain = string.Empty;
                        switch (Slot.FuelType)
                        {
                            case ComponentEnergyMachine.FuelType.Fliud:
                                type = "流体";
                                remain = ILibrary.FormatFluid(Slot.FuelAmount);
                                break;
                            case ComponentEnergyMachine.FuelType.Gas:
                                type = "气体";
                                remain = ILibrary.FormatGas(Slot.FuelAmount);
                                break;
                            case ComponentEnergyMachine.FuelType.Dust:
                                type = "粉末";
                                remain = ILibrary.FormatFloat(Slot.FuelAmount);
                                break;
                            default: break;
                        }
                        MekModLoader.DropBox(this, $"类型：{type}\n名称：{name}\n储存：{remain}");
                    }
                    else MekModLoader.DropBox(this, "空");
                }
            }
        }
    }
}
